<template>
    <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart',
        },
        width: {
            type: String,
            default: '100%',
        },
        height: {
            type: String,
            default: '350px',
        },
        autoResize: {
            type: Boolean,
            default: true,
        },
        scatterData: {
            type: Object,
            required: true,
        },
    },
    data() {
        return {
            chart: null,
        }
    },
    watch: {
        scatterData: {
            deep: true,
            handler(val) {
                console.log('boxData2=========', val)
                this.setOptions(val)
            },
        },
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, null)
            this.setOptions(this.scatterData)
        },
        setOptions(scatterData) {
            console.log('expectedData=========', scatterData)
            const data = scatterData.checkoutValues || []
            const supplierBatch = scatterData.supplierBatch || []
            const average = scatterData.average || []
            let upperLimit = scatterData.maxValue || -1
            let lowerLimit = scatterData.minValue || -1
            let targetValue = scatterData.target || -1
            // if (data.length > 1) {
            //     upperLimit = -1
            //     lowerLimit = -1
            //     targetValue = -1
            // }

            const markLineOpt = {
                label: {
                    // position:'start',
                    formatter: '{b}: {c}',
                    show: true,
                },
                lineStyle: {
                    color: '#D9001B',
                },

                data: [
                    {
                        name: '标准值',
                        // 支持 'average', 'min', 'max'
                        yAxis: targetValue,
                    },
                    {
                        name: '上限值',
                        yAxis: upperLimit,
                        // 支持 'average', 'min', 'max'
                        // type: 'max'
                    },
                    {
                        name: '下限值',
                        yAxis: lowerLimit,
                    },
                ],
            }
            this.chart.clear()
            this.chart.setOption({
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        animation: false,
                    },
                    formatter: ' {a}:  ({c})',
                },
                dataZoom: [
                    {
                        type: 'inside',
                        start: 0,
                        end: 20,
                    },
                    {
                        show: true,
                        type: 'slider',
                        top: '90%',
                        xAxisIndex: [0],
                        start: 0,
                        end: 20,
                    },
                ],
                toolbox: {
                    show: true,
                    feature: {
                        saveAsImage: {
                            show: true,
                            backgroundColor: '#fff',
                        },
                    },
                    iconStyle: {
                        borderColor: '#c0c3cd',
                    },
                },
                xAxis: [
                    {
                        gridIndex: 0,
                        data: supplierBatch,
                        name: '供应商批次',
                    },
                    // { gridIndex: 1, min: 0, max: 20 },
                    // { gridIndex: 2, min: 0, max: 20 },
                    // { gridIndex: 3, min: 0, max: 20 }
                ],
                yAxis: [
                    {
                        gridIndex: 0,
                        min: 0,
                        max: 50,
                        name: '检验值',
                    },
                    // { gridIndex: 1, min: 0, max: 15 },
                    // { gridIndex: 2, min: 0, max: 15 },
                    // { gridIndex: 3, min: 0, max: 15 }
                ],
                legend: {
                    top: 0,
                    left: 'center',
                    itemWidth: 6, //矩形宽度
                    itemHeight: 6, //矩形宽度
                    data: [
                        { name: '检验值' },
                        { name: '平均值', icon: 'line' },
                    ],
                },
                series: [
                    {
                        name: '检验值',
                        type: 'scatter',
                        // xAxisIndex: 0,
                        // yAxisIndex: 0,
                        data: data,
                        markLine: markLineOpt,
                    },
                    {
                        name: '平均值',
                        data: average,
                        type: 'line',
                        smooth: true,
                    },
                ],
            })
        },
    },
}
</script>
